package com.woc.boot.admin.entity;

import com.woc.boot.admin.constant.ErrorCode;

/**
 * 返回结果
 * 
 * @author     woc
 * @date       2024-01-26 
 * @version    0.0.1
*/
public class Response<T> {
	private String code;
	private String message;
	private Long timestamp;
	private T data;

	public boolean isOk() {
		return "200".equals(this.code) || "0200".equals(this.code);
	}
	public Response<T> withOk() {
		code = "0200";
		message = "成功";
		return this;
	}
	public Response<T> withOk(String status, String msg) {
		this.code = status;
		message = msg;
		return this;
	}
	public Response<T> withOk(T data) {
		code = "0200";
		message = "成功";
		this.data = data;
		return this;
	}
	public Response<T> withData(T data) {
		code = "0200";
		message = "成功";
		this.data = data;
		return this;
	}
	public Response<T> withError(ErrorCode code) {
		this.code = "0" + code.getCode();
		this.message = code.getDesc();
		return this;
	}
	public Response<T> withError(String errMsg) {
		this.code = String.format("0%d", ErrorCode.Fail.getCode());
		this.message = errMsg;
		return this;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(Long timestamp) {
		this.timestamp = timestamp;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	@Override
	public String toString() {
		return "Response{" +
				"code='" + code + '\'' +
				", message='" + message + '\'' +
				", timestamp=" + timestamp +
				", data=" + data +
				'}';
	}
}
